package cn.edu.zjicm.wordsnet_d.data.test.selection;

import java.util.List;
import java.util.Vector;

import android.widget.TextView;

import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.net.Client;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;

public class Collocation extends QuestionMaker {
	// private int node_id;
	private String rightChoice;
	private String promopt = "";
	private List<Integer> used = new Vector<Integer>(4);
	
	Collocation(WordFactory wf){
		super(wf);
	}

	@Override
	public String getQuestion(OneWord word) {
		// TODO Auto-generated method stub
		String ret = null;
		used.clear();
		used.add(word.getIndex_word_id());

		rightChoice = word.getLemma();

		String sql = " from colloc_cet4 where Index_word_id="+ word.getIndex_word_id();
		int total = wf.getInt("select count(Colloc_cn) " + sql);

		String colloc_eng;
		String colloc_cn;
		if (total > 0 && Math.random() > -1) {//-1 for always
			int limit = WordFactory.rand(total);
			// node_id=wf.getInt("select node_id "+sql);
			colloc_eng = wf.getStringBySQL("select Colloc_eng " + sql
					+ " limit " + limit + ",1");
			colloc_cn = wf.getStringBySQL("select Colloc_cn " + sql + " limit "
					+ limit + ",1");

		} else {
			colloc_cn = "网络连接失败";
			colloc_eng = "network fail";

			// /通过网络取得colloc_cn,colloc_eng
			String[] col = Client.getCollocation(word.getIndex_word_id());
			if (col[0] != null)
				colloc_eng = col[0];
			if (col[1] != null)
				colloc_cn = col[1] + "(from network)";

		}
		ret = StringUtil.hideWord(colloc_eng, word.getLemma()) + colloc_cn;
		promopt = colloc_eng + colloc_cn;

		return ret;
	}

	@Override
	public String getRightChoice(OneWord word) {
		// TODO Auto-generated method stub
		return rightChoice;
	}

	@Override
	public void promopt(TextView tv) {
		// TODO Auto-generated method stub
		tv.setText(promopt);
	}

}
